﻿using Company.Core.Attributes;
using Company.Core.Language;
using Prism.Events;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Company.Core.IOC
{
    [ExposedService(AutoInitialize = true)]
    public class PrismProvider
    {
        private static IContainerExtension _container;
        private static IModuleManager _moduleManager;
        private static IRegionManager _regionManager;
        private static IDialogService _dialogService;
        private static IEventAggregator _eventAggregator;
        private static ILanguageManager _languageManager;
        public PrismProvider(
            IContainerExtension container
            , IModuleManager moduleManager
            , IRegionManager regionManager
            , IDialogService dialogService
            , IEventAggregator eventAggregator
            , ILanguageManager languageManager
            )
        {
            _container = container;
            _moduleManager = moduleManager;
            _regionManager = regionManager;
            _dialogService = dialogService;
            _eventAggregator = eventAggregator;
            _languageManager = languageManager;
        }

        /// <summary>
        /// IOC容器 注册+获取
        /// </summary>
        public static IContainerExtension Container => ResolveIfNull(ref _container);

        /// <summary>
        /// 模块管理
        /// </summary>
        public static IModuleManager ModuleManager => ResolveIfNull(ref _moduleManager);

        /// <summary>
        /// 区域管理器
        /// </summary>
        public static IRegionManager RegionManager => ResolveIfNull(ref _regionManager);

        /// <summary>
        /// 弹窗服务
        /// </summary>
        public static IDialogService DialogService => ResolveIfNull(ref _dialogService);

        /// <summary>
        /// 事件聚合器
        /// </summary>
        public static IEventAggregator EventAggregator => ResolveIfNull(ref _eventAggregator);

        public static ILanguageManager LanguageManager => ResolveIfNull(ref _languageManager);

        public static Dispatcher Dispatcher => Application.Current.Dispatcher;

        public static Window MainWindow => Application.Current.MainWindow;

        private static T ResolveIfNull<T>(ref T instance)
        {
            instance = instance ?? ContainerLocator.Current.Resolve<T>();
            return instance;
        }
    }
}
